<template>
    <van-form @submit="onSubmit">
        <van-cell-group inset>
            <van-field
                    v-model="username"
                    name="username"
                    label="昵称"
                    placeholder="昵称"
                    :rules="[{ required: true, message: '请填写昵称' }]"
            />
            <van-field
                    v-model="userAccount"
                    name="userAccount"
                    label="账号"
                    placeholder="账号"
                    :rules="[{ required: true, message: '请填写账号' }]"
            />
            <van-field
                    v-model="userPassword"
                    type="password"
                    name="userPassword"
                    label="密码"
                    placeholder="请输入密码"
                    :rules="[{ required: true, message: '请填写密码' }]"
            />
            <van-field
                    v-model="checkPassword"
                    type="password"
                    name="checkPassword"
                    label="密码"
                    placeholder="请再次输入密码"
                    :rules="[{ required: true, message: '请再次密码' }]"
            />
        </van-cell-group>
        <div style="margin: 16px;">
            <van-button round block type="primary" native-type="submit">
                注册
            </van-button>
        </div>
    </van-form>

</template>

<script setup lang="ts">
    import {useRouter} from "vue-router"
    import {ref} from 'vue'
    import myAxois from "../plugin/MyAxios";
    import {showFailToast, showSuccessToast} from "vant";

    const router = useRouter();

    const username = ref('');
    const userAccount = ref('');
    const userPassword = ref('');
    const checkPassword = ref('');

    //点击登录按钮所触发的事件
    const onSubmit = async () => {
        const res = await myAxois.post('/user/register',{
            username:username.value,
            userAccount:userAccount.value,
            userPassword:userPassword.value,
            checkPassword:checkPassword.value

        })
        if(res.data.code === 0){
            showSuccessToast("注册成功");
            router.replace("/");
        }else{
            showFailToast(res.data.msg)
        }
    }

</script>

<style scoped>

</style>